package com.liang.paul.officelite.activities;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.dd.processbutton.iml.ActionProcessButton;
import com.liang.paul.officelite.R;
import com.liang.paul.officelite.beams.OfficeUser;

import cn.bmob.v3.listener.SaveListener;

public class RegisterActivity extends FastActivity implements View.OnClickListener {

    EditText name, email, phone, password, pwdconfirm;
    ActionProcessButton btn_regist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
    }

    private void initView() {
        name = f(R.id.editText_regist_name);
        email = f(R.id.editText_regist_email);
        phone = f(R.id.editText_regist_phone);
        password = f(R.id.editText_regist_pwd);
        pwdconfirm = f(R.id.editText_regist_pwdconfirm);
        btn_regist = f(R.id.btn_regist_regist);
        btn_regist.setMode(ActionProcessButton.Mode.ENDLESS);
        btn_regist.setOnClickListener(this);
    }

    public void onClick(View view){
        btn_regist.setProgress(1);
        if(name.getText().toString().equals("")
                || phone.getText().toString().equals("")
                || email.getText().toString().equals("")
                || password.getText().toString().equals("")
                || pwdconfirm.getText().toString().equals("")){
            btn_regist.setProgress(0);
            Toast.makeText(this, "内容填写不完整", Toast.LENGTH_SHORT).show();
        }else if(!password.getText().toString().equals(pwdconfirm.getText().toString())){
            btn_regist.setProgress(0);
            Toast.makeText(this, "两次密码不一致", Toast.LENGTH_SHORT).show();
        }else {
            login();
        }
    }

    private void login() {
        OfficeUser user = new OfficeUser();
        user.setUsername(name.getText().toString());
        user.setEmail(email.getText().toString());
        user.setMobilePhoneNumber(phone.getText().toString());
        user.setPassword(password.getText().toString());
        user.setEmailVerified(false);
        user.setMobilePhoneNumberVerified(false);
        user.signUp(this, new SaveListener() {
            @Override
            public void onSuccess() {
                Toast.makeText(getApplicationContext(), "验证邮件已发送，请前往验证", Toast.LENGTH_SHORT).show();
                finish();
            }

            @Override
            public void onFailure(int i, String s) {
                btn_regist.setProgress(0);
                Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
            }
        });
    }


}
